package com.util.common.sql;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import org.apache.commons.lang.StringUtils;

public abstract class AbstractSQLBlock implements SQLBlock {
	
	protected String tableName;
//	private SearchBlock searchBlock = new SearchBlockImp();
	
	@Override
	public void setTableName(String tableName) {
		this.tableName = tableName;
	}
	
	public String mergeWhere(Map<String,Object> map){
		Map<String,Object> stempMap = new HashMap<String,Object>();
		stempMap.putAll(map);
		String sqlStr = " where ";
		if(stempMap == null || stempMap.size() < 1){
			return "";
		}
		String termsLink = (String)stempMap.get("termsLink");
		if(termsLink == null || "".equals(termsLink.trim())){
			//如果 无连接值，则设置默认为and
			termsLink = "and";
		}else{
			stempMap.remove("termsLink");
		}
		String orderBy = (String) stempMap.get("orderBy");
		stempMap.remove("orderBy");
		
		for(Iterator<String> itr = stempMap.keySet().iterator(); itr.hasNext() ; ){
			String key = itr.next();
			Object obj = stempMap.get(key);
			if(obj instanceof Map<?,?> ){
				//如果参数类型为stempMap则获得key作为连接条件，value作为值
				Object subTerms = ((Map<?,?>)obj).get("termsLink");
				subTerms = subTerms != null ? subTerms : termsLink;
				sqlStr += key.trim()+" "+((Map<?,?>)obj).get("key")+" "+((Map<?,?>)obj).get("value")+" "+subTerms+" ";
			}else{
				sqlStr += key.trim()+"="+stempMap.get(key)+" "+termsLink+" ";
			}
		}
		sqlStr = (sqlStr.lastIndexOf(termsLink) > -1) ? 
							sqlStr.substring(0,sqlStr.lastIndexOf(termsLink)) : sqlStr;
		if(orderBy != null && !"".equals(orderBy)){
			if("where".equals(sqlStr.trim())){
				sqlStr = " ORDER BY "+orderBy;
			}else{
				sqlStr += " ORDER BY "+orderBy;
			}
		}
		return sqlStr;
	}
	
	@Override
	public String mergeWhere(String searchId, String searchValue) {
		String sql = " where ";
		sql += searchId + "="+searchValue+" ";
		return sql;
	}
	
	@Override
	public String mergeWhere(String searchId,String searchValue,String andOr){
		String sql = " where ";
		if( searchValue == null || "".equals(searchValue.trim()) ){	return ""; }
		String[] value = searchValue.split(",");
		for(int i = 0;i < value.length - 1 && !"".equals(value[i]); i++){//少加一个，后面添加
			sql += searchId + "=" + value[i] + " " + andOr+" ";
		}
		//添加最后一个
		sql += "".equals(value[value.length -1]) ? "" : (searchId + "=" + value[value.length -1] + " ");
		
		return sql;
	}
	
	//like
	
	@Override
	public String mergeLike(String key,String value){
		return ( key + " like '" + value + "' " );
	}
	
	//select
	
	@Override
	public String select(){
		String sql = "select * from ";
		sql += this.tableName + " ";
		return sql;
	}
	
	@Override
	public String select(String value){
		String sql = (value == null) ? this.select() : (" select " + value + " from ");
		sql += this.tableName + " ";
		return sql;
	}
	
	@Override
	public String mergeSelect(String searchId, String searchValue) {
		String sql = this.select();
		sql += this.tableName + " " + this.mergeWhere(searchId, searchValue);
		return sql;
	}
	
	//delete
	
	@Override
	public String delete(){
		String sql = "delete from " + this.tableName + " ";
		return sql;
	}
	
	@Override
	public String mergeDelete(String searchId,String searchValue){
		String sql = this.delete();
		sql += this.mergeWhere(searchId, searchValue);
		return sql;
	}
	
	@Override
	public String mergeDelete(String searchId,String searchValue,String andOr){
		//为了防止删除所有记录，当searchValue为null或者为空值时返回""
		if(StringUtils.isBlank(searchValue)){ return ""; }
		String sql = this.delete();
		sql += this.mergeWhere(searchId, searchValue, andOr);
		return sql;
	}
	
	@Override
	public String mergeDeleteIN(String searchId,String searchValue){
		//为了防止删除所有记录，当searchValue为null或者为空值时返回""
		if(StringUtils.isBlank(searchValue)){ return ""; }
		String sql = this.delete();
		sql += " WHERE " + searchId + " IN (" + searchValue + " )";
		return sql;
	}
	
	//update
	
	@Override
	public String update(){
		return " update "+this.tableName+" ";
	}
	
	@Override
	public String mergeUpdate(String updateField,String updateValue,
			String searchId,String searchValue){
		String sql = this.update();
		sql += " set " + updateField + "=" + updateValue+" ";
		sql += this.mergeWhere(searchId, searchValue);
		return sql;
	}
	
	@Override
	public String mergeUpdate(String updateField,String updateValue,
			String searchId,String searchValue,String andOr){
		//为了防止更新所有记录，当searchValue为null或者为空值时返回""
		if(searchValue == null || "".equals(searchValue.trim())){ return ""; }
		String sql = this.update();
		sql += " set " + updateField + "=" + updateValue+ " ";
		sql += this.mergeWhere(searchId, searchValue, andOr);
		return sql;
	}
}
